<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/9 0009
 * Time: 下午 2:46
 */
namespace app\admin\controller;


use think\Cache;
use app\admin\model\Module;
use app\admin\model\Tenant;
use app\admin\model\User;
use app\common\model\Order;
use app\common\model\Refundorder;
use app\common\model\Usercommission;
use think\Controller;
use think\Db;
use think\Request;

class Index extends Base {

    public function index(){
        
        $tenant = Tenant::field('name,stopUseDate,createDate,authTime,balance')->where('id',session('TENANT_ID'))->find()->toArray();   //商户基本信息
        $tenant['days'] = intval((strtotime($tenant['stopUseDate']) - time())/86400);
        
        $startMonth = strtotime(date("Y-m-01"));
        $endMonth = strtotime(date("Y-m-01 23:59:59")." +1 month -1 day");
        $startDay = strtotime(date('Y-m-d'),time());

        //统计订单数量和订单收入
        $orderToday= $this->orderCount('create_time',[$startDay,$startDay+86400]);
        $orderMonth= $this->orderCount('create_time',[$startMonth,$endMonth]);

        $end = strtotime(date('Y-m-d'),time());
        $start = $end - 86400*6;
        $chart = $this->dateCount($start,$end);
        // dump($chart);die;

        $where1 = ['status'=>2,'tenantId'=>session('TENANT_ID')];  //订单查询
        $where2 = ['status'=>4,'tenantId'=>session('TENANT_ID')];  //售后查询
        $where3 = ['status'=>1,'tenantId'=>session('TENANT_ID')];  //提现处理查询
        if(session('member')['roleType'] == 2) {
            $where1['storeId'] = session('member')['storeId'];
            $where2['storeId'] = session('member')['storeId'];
        }
        $data = [
            'tenant' => $tenant,
            'count' => [
                'userTodayCount' => $this->userCount('create_time',[$startDay,$startDay+86400]),
                'userMonthCount' => $this->userCount('create_time',[$startMonth,$endMonth]),
                'orderTodayCount' => $orderToday['count'],
                'orderMonthCount' => $orderMonth['count'],
                'orderTodayMoney' => $orderToday['money'],
                'orderMonthMoney' => $orderMonth['money'],
                'orderDisCount' => Order::where($where1)->count(),
                'RefundCount' => Refundorder::where($where2)->count(),
                'tixianCount' => Usercommission::where($where3)->count(),
            ],
            'chart' => $chart,
            'search' => ['start'=>$start,'end'=>$end]
        ];

        //根据日期查询会员数、订单数、收入额（元）

        $this->assign('info',$data);
        // dump($data);die;
        return $this->fetch();
    }

    public function chartApi() {
        $request = Request::instance();
        $param = $request->param();
        $start = strtotime($param['start']);
        $end = strtotime($param['end']);
        // return $start;
        $chart = $this->dateCount($start,$end);
        return json($chart);
    }

    //统计会员数量
    private function userCount($field,$option){
        return User::whereBetween($field,$option)->where('tenantId',session('TENANT_ID'))->count();
    }

    //统计订单数量和金额
    private function orderCount($field,$option){
        if(session('member')['roleType'] == 2) {
            $where = ['tenantId'=>session('TENANT_ID'),'storeId'=>session('member')['storeId']];
        } else {
            $where = ['tenantId'=>session('TENANT_ID')];
        }
        $order = Order::whereBetween($field,$option)->where($where)->select();
        $count = 0;
        $money = 0;
        foreach ($order as $v) {
            $count++;
            if(($v['linkType'] == 0 || $v['linkType'] == 1) && $v['status'] != 1) {
                $money  += $v['total'];
            }
        }
        return ['count'=>$count,'money'=>$money];
    }

    //统计图表数据
    private function dateCount($start,$end){
        $x_option = [];
        $user = [];
        $order = [];
        $money = [];
        $length = intval(($end-$start)/86400);
        for($i=0; $i <= $length; $i++){
            $startDay = $start + ($i*86400);
            $x_option[] = date('Y-m-d',$startDay);
            $user[] = $this->userCount('create_time',[$startDay,$startDay+86400]);
            $orderRow = $this->orderCount('create_time',[$startDay,$startDay+86400]);
            $order[] = $orderRow['count'];
            $money[] = $orderRow['money']/100;
        }
        return ['x_option'=>json_encode($x_option),'user'=>json_encode($user),'order'=>json_encode($order),'money'=>json_encode($money)];
    }


    public function welcome(){

        dump(session('TENANT_ID'));
        return "auth";
    }
    public function wechatauth(){
        return $this->fetch();
    }

}